
/**
 * 改变iframe的指向
 * 
 * @param {*} newHash 
 */
function doChangeIframe(newHash) {
    let content = newHash.replace("#", "").split("?");
    // 判断路径与标签
    let paths = content[0].split("_");
    let newSrc = ".";
    for (path in paths) {
        newSrc = newSrc + "/";
        newSrc = newSrc + paths[path];
    }
    newSrc = newSrc + ".html";

    if (1 in content) {
        newSrc = newSrc + "#" + content[1];
    }

    console.log("doChangeIframe:" + newHash + "," + newSrc);
    document.getElementById("content").src = newSrc;
}

/**
 * 改变导航栏的激活状态
 * 
 * @param {*} newHash 
 */
function doChangeAciveStatus(newHash) {
    {
        let ulNode = document.getElementById("left_navi_list_object");
        let liNodes = ulNode.getElementsByTagName("li");
        for (let j = 0; j < liNodes.length; j++) {
            if (liNodes[j].getElementsByTagName("a").length > 0) {
                let aNode = liNodes[j].getElementsByTagName("a")[0];
                aNode.className = aNode.className.replace("active", "");
                if (aNode.hash === newHash) {
                    aNode.className += " active";
                }
            }
        }

    }


    {
        let ulNode = document.getElementById("small_navi_list_object");
        let liNodes = ulNode.getElementsByTagName("li");
        for (let j = 0; j < liNodes.length; j++) {
            if (liNodes[j].getElementsByTagName("a").length > 0) {
                let aNode = liNodes[j].getElementsByTagName("a")[0];
                aNode.className = aNode.className.replace("active", "");
                if (aNode.hash === newHash) {
                    aNode.className += " active";
                }
            }
        }
    }
}

/**
 * 检查当前的#标签，判断显示内容
 */
function doChangeHash() {
    let newHash = window.location.hash ? window.location.hash : "#apis_html_introduce";
    doChangeIframe(newHash);
    doChangeAciveStatus(newHash);

    if (isNaviAlert) {
        hideNaviAlert();
    }
}

function doAdaptiveContentHeight() {
    let rightContentIframe = document.getElementById("content");
    rightContentIframe.height = 0;

    try {


        let iframeBodyHeight = rightContentIframe.contentWindow.document.body.scrollHeight;
        let iframeBodyDocumentHeight = rightContentIframe.contentWindow.document.documentElement.scrollHeight;

        let maxHeight = Math.max(iframeBodyHeight, iframeBodyDocumentHeight);
        console.log("doAdaptiveContentHeight success:" + maxHeight);
        rightContentIframe.height = maxHeight;
    } catch (error) {
        let rightContentContainer = document.getElementsByClassName("right_content_container")[0];
        let rightContentContainerHeight = window.getComputedStyle(rightContentContainer).getPropertyValue('height');
        console.log("doAdaptiveContentHeight fail:" + rightContentContainerHeight);
        rightContentIframe.height = rightContentContainerHeight;
    }
}



// 监听 hash 变化
window.onhashchange = function (event) {
    doChangeHash();
}
doChangeHash();


// 监听 iframe 内容变更
let iframe = document.getElementById("content");
iframe.onload = function (event) {
    doAdaptiveContentHeight();
}

doAdaptiveContentHeight();

var isNaviAlert = false;
/**
 * 显示导航弹窗
 */
function toggleNaviAlert() {
    if (isNaviAlert) {
        hideNaviAlert();
    }
    else {
        showNaviAlert();
    }
}

/**
 * 显示导航弹窗
 */
function showNaviAlert() {
    let smallNavContainerBg = document.getElementById("small_navi_container_bg");
    smallNavContainerBg.style.position = "fixed";

    let naviAlert = document.getElementById("navi_alert");
    naviAlert.style.display = "block";
    isNaviAlert = true;
}

/**
 * 关闭导航弹窗
 * 
 */
function hideNaviAlert() {
    let smallNavContainerBg = document.getElementById("small_navi_container_bg");
    smallNavContainerBg.style.position = "relative";
    smallNavContainerBg.scrollIntoView();

    let naviAlert = document.getElementById("navi_alert");
    naviAlert.style.display = "none";
    isNaviAlert = false;

}